package com.antunnel.betting.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.antunnel.betting.service.OldSSCBettingService;
import com.antunnel.common.orm.BaseServiceImpl;
import com.antunnel.common.util.Combination;
import com.antunnel.common.util.OldSSCStaticValue;
import com.google.gson.JsonArray;

/**
 * 老时时彩
 * 
 * @author ning.wang
 * 
 */
public class OldSSCBettingServiceImpl extends BaseServiceImpl implements
		OldSSCBettingService {
	/**
	 * 五星通选 从个、十、百、千、万位各选1个或多个号码，所选号码与开奖号码一一对应，兼中兼得，即中奖20440元；如所选号码和开奖号码，
	 * 前三位或后三位一一对应， 即中奖220元；如前二位或者后二位一一对应，即中奖20元
	 * 
	 * @param gewei
	 * @param shiwei
	 * @param baiwei
	 * @param qianwei
	 * @param wanwei
	 * @param bonus
	 * @param perbetcost
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getFiveStarTongXuanBetMsg(String gewei,
			String shiwei, String baiwei, String qianwei, String wanwei) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (gewei != null && shiwei != null && baiwei != null
				&& qianwei != null && wanwei != null) {
			Integer geweilength = gewei.trim().length();
			Integer shiweilength = shiwei.trim().length();
			Integer baiweilength = baiwei.trim().length();
			Integer qianweilength = qianwei.trim().length();
			Integer wanweilength = wanwei.trim().length();
			Integer selectedbetcount = geweilength * shiweilength
					* baiweilength * qianweilength * wanweilength;
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			Integer maxbetprice = OldSSCStaticValue.FiveStarTongXuanBetMaxBonus;
			Integer minbetprice = OldSSCStaticValue.FiveStarTongXuanBetMinBonus;
			Integer maxachieve = maxbetprice - totalcost;
			Integer minachieve = minbetprice - totalcost;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("maxbetprice", OldSSCStaticValue.FiveStarTongXuanBetMaxBonus);
			map.put("minbetprice", OldSSCStaticValue.FiveStarTongXuanBetMinBonus);
			map.put("minachieve", minachieve);
			map.put("maxachieve", maxachieve);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.FiveStarTongXuanBetMaxBonus);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 五星直选
	 */
	public Map<String, Object> getFiveStarZhiXuanBetMsg(String gewei,
			String shiwei, String baiwei, String qianwei, String wanwei)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (gewei != null && shiwei != null && baiwei != null
				&& qianwei != null && wanwei != null) {
			Integer geweilength = gewei.trim().length();
			Integer shiweilength = shiwei.trim().length();
			Integer baiweilength = baiwei.trim().length();
			Integer qianweilength = qianwei.trim().length();
			Integer wanweilength = wanwei.trim().length();
			Integer selectedbetcount = geweilength * shiweilength
					* baiweilength * qianweilength * wanweilength;
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			Integer betprice = OldSSCStaticValue.FIVESTARTZHIXUANBONUS;
			Integer achieve = betprice - totalcost;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.FIVESTARTZHIXUANBONUS);
			map.put("achieve", achieve);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.FIVESTARTZHIXUANBONUS);
			map.put("achieve", 0);
			return map;
		}

	}

	/**
	 * 一星 从0～9十个号码中任选1个或多个号码，所选号码与开奖号码最后一位号码相同，即中奖10元
	 * 
	 * @param number
	 * @param separator
	 *            号码分割副
	 * @param bonus
	 *            中奖奖金
	 * @param perbetcost
	 *            每注价格
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getOneStarBetMsg(String number)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (number == null) {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.ONESTARBETBONUS);
			map.put("achieve", 0);
			return map;
		} else {
			Integer selectedbetcount = number.trim().length();
			Integer totalcost = OldSSCStaticValue.PERBETCOST * selectedbetcount;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.ONESTARBETBONUS);
			map.put("achieve", OldSSCStaticValue.ONESTARBETBONUS - totalcost);
			return map;
		}
	}

	/**
	 * 三星直选
	 */
	public Map<String, Object> getThreeStarZhiXuanBetMsg(String gewei,
			String shiwei, String baiwei) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (gewei != null && shiwei != null && baiwei != null) {
			Integer geweilength = gewei.trim().length();
			Integer shiweilength = shiwei.trim().length();
			Integer baiweilength = baiwei.trim().length();
			Integer selectedbetcount = geweilength * shiweilength
					* baiweilength;
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			Integer betprice = OldSSCStaticValue.THREESTARTZHIXUANBONUS;
			Integer achieve = betprice - totalcost;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.THREESTARTZHIXUANBONUS);
			map.put("achieve", achieve);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.THREESTARTZHIXUANBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 三星组六
	 */
	public Map<String, Object> getThreeStarZuLiuBetMsg(String number)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (number != null && number.length() > 2) {
			Integer selectedbetcount = this.getThreeStarZuLiuList(number)
					.size();
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			Integer betprice = OldSSCStaticValue.THREESTARTZULIUBONUS;
			Integer achieve = betprice - totalcost;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.THREESTARTZULIUBONUS);
			map.put("achieve", achieve);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.THREESTARTZULIUBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 三星组三
	 * 
	 * @param number
	 */
	public Map<String, Object> getThreeStarZuSanBetMsg(String number)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (number != null && number.length() > 1) {
			Integer selectedbetcount = this.getThreeStarZuSanList(number)
					.size();
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			Integer betprice = OldSSCStaticValue.THREESTARTZUSANBONUS;
			Integer achieve = betprice - totalcost;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.THREESTARTZUSANBONUS);
			map.put("achieve", achieve);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.THREESTARTZUSANBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 二星直选 从个、十位各选1个或多个号码，所选号码与开奖号码后两位号码相同（且顺序一致），即中奖100元
	 * 
	 * @param number
	 * @param separator
	 *            号码分割副
	 * @param bonus
	 *            中奖奖金
	 * @param perbetcost
	 *            每注价格
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getTwoStarZhiXuanBetMsg(String shiwei,
			String gewei) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (shiwei != null && gewei != null) {
			Integer shiweilength = shiwei.trim().length();
			Integer geweilength = gewei.trim().length();
			Integer selectedbetcount = shiweilength * geweilength;
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.TWOSTARTZHIXUANBONUS);
			map.put("achieve", OldSSCStaticValue.TWOSTARTZHIXUANBONUS
					- totalcost);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.TWOSTARTZHIXUANBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 二星组选 从个、十位各选1个或多个号码，所选号码与开奖号码后两位相同（顺序不限，不含对子号），即中奖50元
	 * 
	 * @param number
	 */
	public Map<String, Object> getTwoStarZuXuanBetMsg(String shiwei,
			String gewei) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (shiwei != null && gewei != null) {
			Integer shiweilength = shiwei.trim().length();
			Integer geweilength = gewei.trim().length();
			Integer selectedbetcount = shiweilength * geweilength;
			Integer totalcost = selectedbetcount * OldSSCStaticValue.PERBETCOST;
			map.put("selectedbetcount", selectedbetcount);
			map.put("totalcost", totalcost);
			map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANBONUS);
			map.put("achieve", OldSSCStaticValue.TWOSTARTZUXUANBONUS
					- totalcost);
			return map;
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 二星组选复式 从0～9中任选3～6个号码，所选号码与开奖号码后两位号码相同（顺序不限，不含对子号），即中奖50元
	 * 
	 * @param number
	 * @param separator
	 *            号码分割副
	 * @param bonus
	 *            中奖奖金
	 * @param perbetcost
	 *            每注价格
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getTwoStarZuXuanFuShiBetMsg(String number)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (number != null) {
			Integer numberlength = number.trim().length();
			if (numberlength >= 3 && numberlength <= 6) {
				Integer selectedbetcount = Combination.getCombination(
						new int[numberlength], 2).size();
				Integer totalcost = selectedbetcount
						* OldSSCStaticValue.PERBETCOST;
				map.put("selectedbetcount", selectedbetcount);
				map.put("totalcost", totalcost);
				map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS);
				map.put("achieve", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS
						- totalcost);
				return map;
			} else if (numberlength < 3) {
				map.put("selectedbetcount", 0);
				map.put("totalcost", 0);
				map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS);
				map.put("achieve", 0);
				return map;
			} else {
				Integer selectedbetcount = Combination.getCombination(
						new int[6], 2).size();
				Integer totalcost = selectedbetcount
						* OldSSCStaticValue.PERBETCOST;
				map.put("selectedbetcount", selectedbetcount);
				map.put("totalcost", totalcost);
				map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS);
				map.put("achieve", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS
						- totalcost);
				return map;
			}
		} else {
			map.put("selectedbetcount", 0);
			map.put("totalcost", 0);
			map.put("betprice", OldSSCStaticValue.TWOSTARTZUXUANFUSHIBONUS);
			map.put("achieve", 0);
			return map;
		}
	}

	/**
	 * 列出三星组六的所有选号
	 * 
	 * @param number
	 * @return
	 */
	public List<Map<String, Object>> getThreeStarZuLiuList(String number) {
		List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
		String gewei = null;
		String shiwei = null;
		String baiwei = null;
		if (number != null) {
			for (int i = 0; i < number.length() - 2; i++) {
				baiwei = number.charAt(i) + "";
				for (int j = i + 1; j < number.length() - 1; j++) {
					shiwei = number.charAt(j) + "";
					for (int k = j + 1; k < number.length(); k++) {
						gewei = number.charAt(k) + "";
						maplist.add(this.createFiveNum(gewei, shiwei, baiwei,
								null, null));
					}
				}
			}
			return maplist;
		} else {
			return null;
		}
	}

	/**
	 * 列出三星组三的所有选号
	 * 
	 * @param number
	 * @return
	 */
	public List<Map<String, Object>> getThreeStarZuSanList(String number) {
		List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
		if (number != null) {
			for (int i = 0; i < number.length() - 1; i++) {
				for (int j = i + 1; j < number.length(); j++) {
					maplist.add(this.createFiveNum(number.charAt(j) + "",
							number.charAt(i) + "", number.charAt(i) + "", null,
							null));
					maplist.add(this.createFiveNum(number.charAt(j) + "",
							number.charAt(j) + "", number.charAt(i) + "", null,
							null));
				}
			}
			return maplist;
		} else {
			return null;
		}

	}

	public Map<String, Object> createFiveNum(String gewei, String shiwei,
			String baiwei, String qianwei, String wanwei) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gewei", gewei);
		map.put("shiwei", shiwei);
		map.put("baiwei", baiwei);
		map.put("qianwei", qianwei);
		map.put("wanwei", wanwei);
		return map;
	}

	public static void main(String[] args) throws Exception {
		OldSSCBettingServiceImpl old = new OldSSCBettingServiceImpl();
		List<Map<String, Object>> listmap = old.getThreeStarZuLiuList("34567");
		System.out.println(listmap);

	}

}
